package control;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.NameValuePair;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bettertogether.R;

public class Main extends ListActivity {
	ListView l1;
	ListView l2;
	TextView t1;
	TextView t2;
	Boolean control = false;
	Timer timer;
	HashMap<String, List<Person>> listDataChild;
	ExpandableListView expListView;
	Contact_ExpandableListAdapter listAdapter;
	String[] listDataHeader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		l2 = (ListView) findViewById(R.id.list2);
		t1 = (TextView) findViewById(R.id.textRehber);
		t2 = (TextView) findViewById(R.id.textSonuc);

		EditText search = (EditText) findViewById(R.id.textSearch);
		search.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					if (control) {
						timer.cancel();
						control = false;
					} else {
					//	l1.setVisibility(View.GONE);
						t1.setVisibility(View.GONE);
						l2.setVisibility(View.VISIBLE);
						t2.setVisibility(View.VISIBLE);
					}
					if (control) {
						timer.cancel();
						control = false;
					}
					Starter(3);
				} else if (s.length() <= 0) {
					if (control) {
						timer.cancel();
						control = false;
					}
					//l1.setVisibility(View.VISIBLE);
					t1.setVisibility(View.VISIBLE);
					l2.setVisibility(View.GONE);
					t2.setVisibility(View.GONE);
				}
			}
		});
		expListView = (ExpandableListView) findViewById(R.id.lvExp);

		// preparing list data
		prepareListData();
		listAdapter = new Contact_ExpandableListAdapter(this, listDataHeader,
				listDataChild);

		// setting list adapter
		expListView.setAdapter(listAdapter);

		// Listview Group click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// Toast.makeText(getApplicationContext(),
				// "Group Clicked " + listDataHeader.get(groupPosition),
				// Toast.LENGTH_SHORT).show();
				return false;
			}
		});

		// Listview Group expanded listener
		expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				Toast.makeText(getApplicationContext(),
						listDataHeader[groupPosition] + " Expanded",
						Toast.LENGTH_SHORT).show();
			}
		});

		// Listview Group collasped listener
		expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				Toast.makeText(getApplicationContext(),
						listDataHeader[groupPosition] + " Collapsed",
						Toast.LENGTH_SHORT).show();

			}
		});

		// Listview on child click listener
		expListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO: make link to an issue page
				session.person = session.contactlist.get(childPosition);
				Intent i = new Intent(getApplicationContext(), Detail.class);
				startActivity(i);

				return false;
			}
		});
	}

	public void Starter(int seconds) {
		timer = new Timer();
		control = true;
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				timer.cancel();
				control = false;
				Main.this.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						searchList();
						List2();
					}
				});

			}

		}, seconds * 400); // subsequent rate
	}

	public void searchList() {

	}

	public void List1() {

	}

	public void List2() {

	}

	private void prepareListData() {
		listDataChild = new HashMap<String, List<Person>>();
		// Adding child data
		List<Person> my = new ArrayList<Person>();

		Collections.sort(session.contactlist, new Comparator<Person>() {
			public int compare(Person person1, Person person2) {
				return person1.getName().compareTo(person2.getName());
			}
		});

		char group = ' ';
		int i = 0;
		for (Person person : session.contactlist) {
			if (group != person.getName().charAt(0)) {
				group = person.getName().charAt(0);
				my.clear();
				listDataChild.put(String.valueOf(group).toUpperCase(), my);
				listDataHeader[i] = String.valueOf(group);
				i++;
			} else {
				listDataChild.get(String.valueOf(group)).add(person);
			}

		}

	}

	class LoadAllPeople extends AsyncTask<String, String, String> {
		private ProgressDialog progressDialog;

		@Override
		protected String doInBackground(String... params) {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			JSONHttpClient jsonHttpClient = new JSONHttpClient();
			Person[] contacts = jsonHttpClient.Get("", nameValuePairs,
					Person[].class);
			if (contacts.length > 0) {
				for (Person person : contacts) {
					session.contactlist.add(person);
				}

			} else {

			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute(); // To change body of overridden methods use
									// File |
									// Settings | File Templates.
			// progressDialog = new ProgressDialog(Main.class);
			// progressDialog.setMessage("Loading products. Please wait...");
			// progressDialog.show();
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();

		}
	}
}
